package org.wuliu.Controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.wuliu.dao.*;
import org.wuliu.pojo.*;
import org.wuliu.utils.*;

import java.util.List;

@Controller
@RequestMapping("/dingqv")
@CrossOrigin(value="*")
public class DingqvController {
    @Autowired
    private DingqvDao dingqvDao;
    @Autowired
    private FenqvDao fenqvDao;
    @RequestMapping("/show")
    @ResponseBody
    public ResponseResult getshow(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Dingqv dingqv){
        Page<Dingqv> pag= PageHelper.startPage(page,limit);
        List<Dingqv> list=dingqvDao.getList(dingqv);
        return new ResponseResult(pag.getTotal(),list);
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Dingqv dingqv,String ids){
        String[] id=null;
        if(StringUtils.isNotEmpty(ids)){
            id=ids.split(",");
            dingqv.setFenids(id);
        }
        dingqvDao.add(dingqv);
        fenqvDao.upFenqv(id,dingqv.getFuzerenId());
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Dingqv dingqv,String ids){
        String[] id=null;
        if(StringUtils.isNotEmpty(ids)){
            id=ids.split(",");
            dingqv.setFenids(id);
        }
        fenqvDao.updel(dingqv.getId());
        dingqvDao.delete(dingqv.getId());
        dingqvDao.add(dingqv);
        fenqvDao.upFenqv(id,dingqv.getFuzerenId());
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(String idstr){
        String[] ids=null;
        if(StringUtils.isNotEmpty(idstr)){
            ids=idstr.split(",");
        }
        for(int i=0;i<ids.length;i++){
            fenqvDao.updel(Integer.parseInt(ids[i]));
            dingqvDao.delete(Integer.parseInt(ids[i]));
        }
        return ResponseResult.createSuccessResUle();
    }
}
